package de.koller.altitude.engine.physics;

import de.koller.altitude.ANode;
import de.koller.altitude.Altitude;
import de.koller.altitude.engine.entity.MovingObject;
import de.koller.oceanFramework.tree.Child;
import java.util.HashSet;
import java.util.Set;

public class Physics extends ANode {
	
	public static final int PHY_TIME_STEP = 17;
	private long physicsTimeStamp = 0;

	@Child private final MOPhysics moPhysics = new MOPhysics();
	public Set<MovingObject> movingObjects = new HashSet<>();
	
	@Override
	protected void init() {
		super.init();
		
		physicsTimeStamp = Altitude.time();
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		long unprocessedTime = Altitude.time() - physicsTimeStamp;
		while( unprocessedTime >= PHY_TIME_STEP ) {
			stepPhysics();
			physicsTimeStamp += PHY_TIME_STEP;
			unprocessedTime -= PHY_TIME_STEP;
		}
	}
	
	public void stepPhysics() {
		for( MovingObject mo : movingObjects )
			stepPhysics( mo );
	}

	private void stepPhysics( MovingObject mo ) {
		moPhysics.physicStep( mo, PHY_TIME_STEP );
	}
	
}
